package org.nf.mvc.param;

import org.nf.mvc.servlet.ParamTypeHandler;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Parameter;

/**
 * @Author : LLH
 * @create 2021/5/26
 * Servlet Api 的类型转换器
 */
public class ServletApiTypeHandler extends ParamTypeHandler {
    @Override
    protected Object handle(Parameter parameter) {
        // 获取参数的类型
        Class<?> parameterType = parameter.getType();

        if(parameterType.equals(HttpServletRequest.class)){
            return request;
        }else if(parameterType.equals(HttpServletResponse.class)){
            return response ;
        }else if(parameterType.equals(HttpSession.class)){
            return request.getSession();
        }else if(parameterType.equals(ServletContext.class)){
            return request.getServletContext();
        }
        return null;
    }
}
